<template>
  <el-dialog title="分配角色" :visible="showDialog" @close="btnCancel">
    <el-checkbox-group v-model="checkList">
      <el-checkbox v-for="role in rowsList" :key="role.id" :label="role.id">
        {{ role.name }}
      </el-checkbox>
    </el-checkbox-group>
    <template #footer>
      <el-row type="flex" justify="center">
        <el-button size="small" type="primary" @click="btnOK">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { getRoleList } from "@/api/setting";
import { getUserDetailById } from "@/api/user";
import { assignRoles } from "@/api/employees";
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false,
    },
    userId: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      rowsList: [],
      checkList: [],
    };
  },
  created() {
    this.getRoleList();
  },
  methods: {
    async getRoleList() {
      const { rows } = await getRoleList();
      this.rowsList = rows;
    },
    async getUserDetailById(id) {
      const { roleIds } = await getUserDetailById(id);
      this.checkList = roleIds;
    },
    async btnOK() {
      await assignRoles({
        id: this.userId,
        roleIds: this.checkList,
      });
      this.$message.success("分配角色成功");
      this.$emit("update:showDialog", false);
    },
    btnCancel() {
      this.$emit("update:showDialog", false);
    },
  },
};
</script>

<style>
</style>